package com.newsystem.service.impl;

import com.newsystem.MyUtils.CommonResult;
import com.newsystem.MyUtils.MyUUIDUtils;
import com.newsystem.mapper.UserMapper;
import com.newsystem.pojo.Admin;
import com.newsystem.mapper.AdminMapper;
import com.newsystem.pojo.User;
import com.newsystem.pojo.vo.AdminInfo;
import com.newsystem.pojo.vo.GeneralUserInfo;
import com.newsystem.pojo.vo.MediumInfo;
import com.newsystem.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 管理员 服务实现类
 * </p>
 *
 * @author chenlitao
 * @since 2022-11-06
 */
@Service
//@RequiredArgsConstructor
public class AdminService extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AdminMapper adminMapper;

    //管理员注册账号
    public CommonResult<Object> insertOneAdmin(User user,Admin admin) {
        List<User> users = userMapper.selectByAccount(user.getAccount());
        if (users.size()==1){
            return CommonResult.failed("此账号已存在");
        }
        else {
            user.setId(MyUUIDUtils.getUUID());
            user.setStatue(1);
            if(userMapper.insert(user) > 0){
                admin.setId(MyUUIDUtils.getUUID());
                admin.setUserId(user.getId());
                if(adminMapper.insert(admin) > 0){
                    return CommonResult.success("注册成功");
                }
                else {
                    userMapper.deleteById(user.getId());
                }
            }
        }
        return CommonResult.failed("注册失败");
    }

    //根据管理员id查询管理员信息
    public CommonResult<Object> selectAdminInfoByAdminUserId(String adminUserId){
        AdminInfo adminInfo = adminMapper.selectByAdminUserId(adminUserId);
        if(adminInfo==null){
            return CommonResult.failed("获取失败");
        }
        if(adminInfo.getUser().getStatue()!=1){
            return CommonResult.failed("该账号已经注销");
        }
        return CommonResult.success(adminInfo);
    }

    //普通用户
    public CommonResult<Object> selectAllGeneralUser(Integer startpage,Integer pageSum){
        List<GeneralUserInfo> generalUserInfos = adminMapper.selectAllGeneralUser(startpage,startpage+pageSum);
        return CommonResult.success(generalUserInfos);
    }

    public CommonResult<Object> selectAllGeneralUserExit(Integer startpage,Integer pageSum){
        List<GeneralUserInfo> generalUserInfos = adminMapper.selectAllGeneralUserExit(startpage,startpage+pageSum);
        return CommonResult.success(generalUserInfos);
    }

    public CommonResult<Object> selectAllGeneralUsered(Integer startpage,Integer pageSum){
        List<GeneralUserInfo> generalUserInfos = adminMapper.selectAllGeneralUsered(startpage,startpage+pageSum);
        return CommonResult.success(generalUserInfos);
    }

    //媒体号
    public CommonResult<Object> selectAllMedium(Integer startpage,Integer pageSum){
        List<MediumInfo> generalUserInfos = adminMapper.selectAllMedium(startpage,startpage+pageSum);
        return CommonResult.success(generalUserInfos);
    }

    public CommonResult<Object> selectAllMediumExit(Integer startpage,Integer pageSum){
        List<MediumInfo> generalUserInfos = adminMapper.selectAllMediumExit(startpage,startpage+pageSum);
        return CommonResult.success(generalUserInfos);
    }

    public CommonResult<Object> selectAllMediumed(Integer startpage,Integer pageSum){
        List<MediumInfo> generalUserInfos = adminMapper.selectAllMediumed(startpage,startpage+pageSum);
        return CommonResult.success(generalUserInfos);
    }


}
